package com.new2i.service.impl;
   
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
  
import com.new2i.dao.able.ScreenQuarterDao;
import com.new2i.model.DoctorEntity; 
import com.new2i.model.ScreenQuarterEntity;
import com.new2i.service.able.ScreenQuarterService; 

/**
 * 筛查季度的业务逻辑处理实现类
 * 
 * @since 2015/09/22
 * @author 2i
 *
 */
@Service("ScreenQuarterService")
public class ScreenQuarterServiceImpl implements ScreenQuarterService {
 
	private static Logger logger = Logger.getLogger(ScreenQuarterServiceImpl.class);
 
	@Autowired
	private ScreenQuarterDao mScreenQuarterDao; 
 
	@Override
	public List<HashMap<String, Object>> selectScreenQuarterList(DoctorEntity doctor) {
		List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		Integer doctor_id = doctor.getDoctor_id();
		Integer page = doctor.getPage();
		if(page == null)
			page = 0;
		if(doctor_id == null || doctor_id == 0) { 
			return list;
		} 
		try { 
			List<HashMap<String, Object>> scDoctorList = mScreenQuarterDao.selectScreenQuarterList(doctor_id, page);
			return scDoctorList;
		}catch (Exception e){
			logger.error(e.getLocalizedMessage()); 
		} 
		return list;
	}

	@Override
	public HashMap<String, Object> insertScreenQuarter(ScreenQuarterEntity scQuarter) {
		HashMap<String, Object> rHashMap = new HashMap<String, Object>();
		Integer doctor_id = scQuarter.getDoctor_id();
		String year = scQuarter.getYear();  
		String quarter = scQuarter.getQuarter();  
		if(doctor_id == null || doctor_id == 0) {
			rHashMap.put("code", 311);
			rHashMap.put("err_msg", "未指定创建机构医生"); 
			return rHashMap;
		}
		if(year == null || year.length() <= 0) {
			rHashMap.put("code", 311);
			rHashMap.put("err_msg", "请输入筛查年份"); 
			return rHashMap;
		}  
		if(quarter == null || quarter.length() <= 0) {
			rHashMap.put("code", 311);
			rHashMap.put("err_msg", "请输入筛查季度"); 
			return rHashMap;
		}   
		try { 
			ScreenQuarterEntity rscQuarter = mScreenQuarterDao.insertScreenQuarter(scQuarter);
			rHashMap.put("sc_doctor_id", rscQuarter.getSc_quarter_id());  
			rHashMap.put("year", year);
			rHashMap.put("quarter", quarter); 
			rHashMap.put("msg", "创建筛查季度成功"); 
		}catch (Exception e){
			logger.error(e.getLocalizedMessage());
			rHashMap.put("code", 321);
			rHashMap.put("err_msg", "创建筛查季度失败"); 
		} 
		return rHashMap; 
	}

	@Override
	public HashMap<String, Object> getScreenQuarterById(ScreenQuarterEntity scQuarter) {
		HashMap<String, Object> rHashMap = new HashMap<String, Object>(); 
		Integer scDoctorId = scQuarter.getSc_quarter_id();  
		if(scDoctorId == null || scDoctorId == 0) {
			rHashMap.put("code", 311);
			rHashMap.put("err_msg", "未指定筛查季度"); 
			return rHashMap;
		}  
		try {  
			HashMap<String, Object> ScreenQuarterInfo = mScreenQuarterDao.getScreenQuarterById(scDoctorId); 
			return ScreenQuarterInfo; 
		}catch (Exception e){
			logger.error(e.getLocalizedMessage());
			rHashMap.put("code", 324);
			rHashMap.put("err_msg", "获取用户出错");
		} 
		return rHashMap;  
	}

	@Override
	public HashMap<String, Object> updateScreenQuarter(ScreenQuarterEntity scQuarter) {
		HashMap<String, Object> rHashMap = new HashMap<String, Object>(); 
		Integer scDoctorId = scQuarter.getSc_quarter_id();  
		String year = scQuarter.getYear();  
		String quarter = scQuarter.getQuarter();  
		if(scDoctorId == null || scDoctorId == 0) {
			rHashMap.put("code", 311);
			rHashMap.put("err_msg", "未指定筛查季度"); 
			return rHashMap;
		}
		if(year == null || year.length() <= 0) {
			rHashMap.put("code", 311);
			rHashMap.put("err_msg", "请输入筛查年份"); 
			return rHashMap;
		}  
		if(quarter == null || quarter.length() <= 0) {
			rHashMap.put("code", 311);
			rHashMap.put("err_msg", "请输入筛查季度"); 
			return rHashMap;
		}  
		try {  
			Integer updateRows = mScreenQuarterDao.updateScreenQuarter(scQuarter); 
			if(updateRows > 0) { 
				rHashMap.put("sc_doctor_id", scDoctorId); 
				rHashMap.put("msg", "修改筛查季度成功");  
				rHashMap.put("year", year); 
				rHashMap.put("quarter", quarter); 
			}else {
				rHashMap.put("code", 322);
				rHashMap.put("err_msg", "修改筛查季度失败"); 
			}     
		}catch (Exception e){
			logger.error(e.getLocalizedMessage());
			rHashMap.put("code", 321);
			rHashMap.put("err_msg", "修改筛查季度信息出错");
		} 
		return rHashMap;  
	}

	@Override
	public HashMap<String, Object> removeScreenQuarter(ScreenQuarterEntity scQuarter) {
		HashMap<String, Object> rHashMap = new HashMap<String, Object>(); 
		Integer sc_quarter_id = scQuarter.getSc_quarter_id();
		Integer doctor_id = scQuarter.getDoctor_id(); 
		if(doctor_id == null || doctor_id == 0) {
			rHashMap.put("code", 311);
			rHashMap.put("err_msg", "未指定当前机构操作医生"); 
			return rHashMap;
		}  
		if(sc_quarter_id == null || sc_quarter_id == 0) {
			rHashMap.put("code", 311);
			rHashMap.put("err_msg", "未指定筛查季度"); 
			return rHashMap;
		}   
		try {   
			Integer updateRows = mScreenQuarterDao.removeScreenQuarter(sc_quarter_id, doctor_id);
			if(updateRows > 0) { 
				rHashMap.put("sc_doctor_id", sc_quarter_id); 
				rHashMap.put("msg", "删除筛查季度成功");  
			}else {
				rHashMap.put("code", 322);
				rHashMap.put("err_msg", "筛查季度已被删除"); 
			}  
		}catch (Exception e){
			logger.error(e.getLocalizedMessage());
			rHashMap.put("code", 321);
			rHashMap.put("err_msg", "删除筛查季度出错");
		} 
		return rHashMap;
	}

	@Override
	public HashMap<String, Object> backupScreenQuarter(ScreenQuarterEntity scQuarter) {
		HashMap<String, Object> rHashMap = new HashMap<String, Object>(); 
		Integer sc_quarter_id = scQuarter.getSc_quarter_id();
		Integer doctor_id = scQuarter.getDoctor_id(); 
		if(doctor_id == null || doctor_id == 0) {
			rHashMap.put("code", 311);
			rHashMap.put("err_msg", "未指定当前机构操作医生"); 
			return rHashMap;
		}  
		if(sc_quarter_id == null || sc_quarter_id == 0) {
			rHashMap.put("code", 311);
			rHashMap.put("err_msg", "未指定筛查季度"); 
			return rHashMap;
		}   
		try {   
			Integer updateRows = mScreenQuarterDao.backupScreenQuarter(sc_quarter_id, doctor_id);
			if(updateRows > 0) { 
				rHashMap.put("sc_doctor_id", sc_quarter_id); 
				rHashMap.put("msg", "还原筛查季度成功");  
			}else {
				rHashMap.put("code", 322);
				rHashMap.put("err_msg", "还原学校已被删除"); 
			}  
		}catch (Exception e){
			logger.error(e.getLocalizedMessage());
			rHashMap.put("code", 321);
			rHashMap.put("err_msg", "还原筛查季度出错");
		} 
		return rHashMap;
	}

	@Override
	public HashMap<String, Object> deleteScreenQuarter(ScreenQuarterEntity scQuarter) {
		HashMap<String, Object> rHashMap = new HashMap<String, Object>(); 
		Integer sc_quarter_id = scQuarter.getSc_quarter_id();
		Integer doctor_id = scQuarter.getDoctor_id(); 
		if(doctor_id == null || doctor_id == 0) {
			rHashMap.put("code", 311);
			rHashMap.put("err_msg", "未指定当前机构操作医生"); 
			return rHashMap;
		}  
		if(sc_quarter_id == null || sc_quarter_id == 0) {
			rHashMap.put("code", 311);
			rHashMap.put("err_msg", "未指定筛查季度"); 
			return rHashMap;
		}   
		try {   
			Integer updateRows = mScreenQuarterDao.deleteScreenQuarter(sc_quarter_id, doctor_id);
			if(updateRows > 0) { 
				rHashMap.put("sc_doctor_id", sc_quarter_id); 
				rHashMap.put("msg", "删除筛查季度成功");  
			}else {
				rHashMap.put("code", 322);
				rHashMap.put("err_msg", "筛查季度已被删除"); 
			}  
		}catch (Exception e){
			logger.error(e.getLocalizedMessage());
			rHashMap.put("code", 321);
			rHashMap.put("err_msg", "删除筛查季度出错");
		} 
		return rHashMap;
	}     
}
